/*
    TwoPageView image viewer
    Copyright (C) 2006 Japan Manoretimedia Lab.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
using System;

namespace TwoPageView
{
    public enum MyTriBool
    {
        False,
        True,
        Indeterminate
    }

    class MyUtil
    {
        public static string PathNormalize(string path)
        {
            if (null == path) {
                throw new System.ArgumentNullException("path");
            }
            return path.Replace(":\\\\", ":\\");
        }
        /*
        public static bool IsPathNormailzed(string path)
        {
            if (null == path) {
                throw new System.ArgumentNullException("path");
            }
            return !path.Contains(":\\\\");
        }
        */

        private int CompareNameAsc(DirectoryOrZipDirInfo l, DirectoryOrZipDirInfo r)
        {
            System.Diagnostics.Debug.Assert(l != null);
            System.Diagnostics.Debug.Assert(r != null);
            return string.Compare(l.Name, r.Name, System.StringComparison.OrdinalIgnoreCase);
        }
        private int CompareNameDesc(DirectoryOrZipDirInfo l, DirectoryOrZipDirInfo r)
        {
            return -CompareNameAsc(l, r);
        }

        private long GetLastWriteTime(string path)
        {
            long tick = 0;
            if (folderNameToTick.ContainsKey(path)) {
                return (long)folderNameToTick[path];
            }
            try {
                if (System.IO.Directory.Exists(path)) {
                    tick = System.IO.Directory.GetLastWriteTime(path).Ticks;
                } else {
                    tick = System.IO.File.GetLastWriteTime(path).Ticks;
                }
                folderNameToTick.Add(path, tick);
            } catch (System.Exception ex) {
                Console.WriteLine("E: GetLastWriteTime {0}", ex);
            }
            return tick;
        }

        private int CompareLastUpdateTimeAsc(DirectoryOrZipDirInfo l, DirectoryOrZipDirInfo r)
        {
            System.Diagnostics.Debug.Assert(l != null);
            System.Diagnostics.Debug.Assert(r != null);
            long tickl = GetLastWriteTime(l.FullName);
            long tickr = GetLastWriteTime(r.FullName);
            if (tickl == tickr) {
                return 0;
            }
            if (tickl < tickr) {
                return -1;
            }
            return 1;
        }
        private int CompareLastUpdateTimeDesc(DirectoryOrZipDirInfo l, DirectoryOrZipDirInfo r)
        {
            return -CompareLastUpdateTimeAsc(l, r);
        }

        System.Collections.Hashtable folderNameToTick = new System.Collections.Hashtable();

        public System.Collections.Generic.List<DirectoryOrZipDirInfo> FolderListSortedByDisplayOrder(DirectoryOrZipDirInfo[] diArray, ItemSortOrder sortOrder)
        {
            System.Collections.Generic.List<DirectoryOrZipDirInfo> rv = new System.Collections.Generic.List<DirectoryOrZipDirInfo>(diArray);
            folderNameToTick.Clear();

            switch (sortOrder) {
            case ItemSortOrder.NameAsc:
                rv.Sort(CompareNameAsc);
                break;
            case ItemSortOrder.NameDesc:
                rv.Sort(CompareNameDesc);
                break;
            case ItemSortOrder.LastUpdateTimeAsc:
                rv.Sort(CompareLastUpdateTimeAsc);
                break;
            case ItemSortOrder.LastUpdateTimeDesc:
                rv.Sort(CompareLastUpdateTimeDesc);
                break;
            default:
                System.Diagnostics.Debug.Assert(false);
                break;
            }
            folderNameToTick.Clear();
            return rv;
        }

    }
}
